package com.lindroy.lib_utils.statics.permission

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

/**
 * @author Lin
 * @date 2021/3/15
 * @function
 */
object PermissionX {

    private const val TAG = "InvisibleFragment"

    fun request(activity: FragmentActivity,vararg permissions:String,callback: PermissionCallback){
        val fragmentManager = activity.supportFragmentManager
        val existedFragment = fragmentManager.findFragmentByTag(TAG)
        val fragment = if (existedFragment != null){
            existedFragment as InvisibleFragment
        }else{
            InvisibleFragment().let {
                fragmentManager.beginTransaction().add(it, TAG).commitNow()
                it
            }
        }
        fragment.requestNow(callback,*permissions)
    }

    fun request(fg: Fragment, vararg permissions:String, callback: PermissionCallback){
        val fragmentManager = fg.childFragmentManager
        val existedFragment = fragmentManager.findFragmentByTag(TAG)
        val fragment = if (existedFragment != null){
            existedFragment as InvisibleFragment
        }else{
            InvisibleFragment().let {
                fragmentManager.beginTransaction().add(it, TAG).commitNow()
                it
            }
        }
        fragment.requestNow(callback,*permissions)
    }

}